package com.axend.aerosense.room.viewmodel;

import android.app.Activity;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.entity.RoomRemindBean;
import com.axend.aerosense.room.entity.VipFunSaveDataBean;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;

public class RoomSettingFunctionViewModel extends CustomBaseViewModel<BaseBean> {

    /**
     * 设置类型
     * 0：功能开关
     * 1：功能开关、Alert
     * 2：功能开关、Notification
     * 3：功能开关、Alert、Notification
     * 4：功能开关 ×2、Notification ×2
     */
    public MutableLiveData<Integer> settingType = new MutableLiveData<>();

    //开关1
    public MutableLiveData<Boolean> functionSwitch = new MutableLiveData<>(false);
    //开关2
    public MutableLiveData<Boolean> functionSwitch2 = new MutableLiveData<>(false);

    private Disposable disposable;
    private LoadingDialog loadingDialog;

    public RoomSettingFunctionViewModel(IBaseView baseViewImpl, ILoadDataResult<BaseBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
    }

    /** 通知设置显示与隐藏 */
    public boolean getNotificationVisibility(int type, boolean switch1, boolean switch2){
        if (type == 4) {
            return switch2;
        } else {
            return (type == 2 || type == 3) && switch1;
        }
    }

    public void saveData(Activity activity, Object bean, String url){
        //数据改变，刷新房间设置
        LiveDatabus.getInstance()
                .with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class)
                .postValue(1);

        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(url)
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.room_set_success);
                        activity.onBackPressed();
                    }

                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance()
                                .build(RouterActivityPath.Login.PAGE_LOGIN_HOME)
                                .withBoolean("jumpMainPage", true)
                                .greenChannel()
                                .navigation();
                        dismissLoadDialog();
                    }

                    @Override
                    public void onError(ApiException e) {
                        //ToastUtils.showLong(R.string.room_set_fail);
                        ToastUtils.showLong(e.getMessage());
                        dismissProgress();
                        e.printStackTrace();
                        activity.onBackPressed();
                    }
                });
    }

    /** 保存久坐提醒数据 */
    public void saveSedentaryData(Activity activity, VipFunSaveDataBean continuitySitBean, VipFunSaveDataBean accumulateSitBean){
        //数据改变，刷新房间设置
        LiveDatabus.getInstance()
                .with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class)
                .postValue(1);

        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        showLoadDialog(activity);
        disposable = EasyHttp.post(UrlApi.URL.SetContinuitySit.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(continuitySitBean))
                .execute(new SimpleCallBack<BaseStringBean>() {
                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance()
                                .build(RouterActivityPath.Login.PAGE_LOGIN_HOME)
                                .withBoolean("jumpMainPage", true)
                                .greenChannel()
                                .navigation();
                        dismissLoadDialog();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showLong(e.getMessage());
                        dismissLoadDialog();
                        e.printStackTrace();
                        activity.onBackPressed();
                    }

                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        setAccumulateSit(accumulateSitBean, activity);
                    }
                });
    }

    private void setAccumulateSit(VipFunSaveDataBean accumulateSitBean, Activity activity){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(UrlApi.URL.SetAccumulateSit.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(accumulateSitBean))
                .execute(new SimpleCallBack<BaseStringBean>() {
                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance()
                                .build(RouterActivityPath.Login.PAGE_LOGIN_HOME)
                                .withBoolean("jumpMainPage", true)
                                .greenChannel()
                                .navigation();
                        dismissLoadDialog();
                    }

                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.showShort(R.string.room_set_fail);
                        ToastUtils.showLong(e.getMessage());
                        e.printStackTrace();
                        dismissLoadDialog();
                        activity.onBackPressed();
                    }

                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        ToastUtils.showShort(R.string.room_set_success);
                        activity.onBackPressed();
                        dismissLoadDialog();
                    }
                });
    }

    private void showLoadDialog(Activity activity){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog.Builder(activity)
                    .setMessage(activity.getString(R.string.common_loading))
                    .setCancelable(true)
                    .create();
        }
        loadingDialog.show();
    }

    private void dismissLoadDialog(){
        if (loadingDialog == null){
            return;
        }
        loadingDialog.dismiss();
    }

    @Override
    public void onLoad() {

    }

    @Override
    protected void onCleared() {
        super.onCleared();
        dismissLoadDialog();
        if (null != disposable && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}
